package cn.yunhe.controller;

import cn.yunhe.entity.User;
import cn.yunhe.entity.UserExamHistory;
import cn.yunhe.service.UserExamHistoryService;
import cn.yunhe.util.DateUtil;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserExamHistoryController {
    @Autowired
    @Qualifier("userExamHistoryService")
    private UserExamHistoryService userExamHistoryService;
    //这个是会员中心：用户考试历史
    @RequestMapping("/getuserexamhistory")
    public ModelAndView getUserExamHistory(HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user  = (User) session.getAttribute("Curr_User");
        List<UserExamHistory> userExamHistoryList = userExamHistoryService.UserExamHistory(user.getUserName());
        mv.addObject("userExamHistoryList",userExamHistoryList);
        mv.setViewName("exam-history");
        return  mv;
    }

    @RequestMapping("/getUserExamInfo")
    @ResponseBody
    public Map getUserExamInfo(int pageSize, int pageNum, String examName, Integer examId, String startTime,HttpSession session){
        Date sTime = null;
        if (startTime !=null && !"".equals(startTime)){
            try {
                sTime = DateUtil.String2DateTime(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        User user = (User) session.getAttribute("Curr_User");
        Map map = new HashMap();
        map.put("userId",user.getUserId());
        map.put("pageSize",pageSize);
        map.put("pageNum", pageNum);
        map.put("examName",examName);
        map.put("examId",examId);
        map.put("startTime",sTime);
        Page page = userExamHistoryService.getUserExamInfo(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/view/toExampast_userList")
    public ModelAndView toExampast_userList(HttpServletRequest request, Integer examId){
        request.setAttribute("examId",examId);
        return new ModelAndView("exampast_userList");
    }

}
